import 'package:flutter/material.dart';
import 'package:ichinesereader/controller/books.dart';
import 'package:ichinesereader/utils/log.dart';
import 'package:get/get.dart';
import 'package:flutter_widget_from_html_core/flutter_widget_from_html_core.dart';

class BookFullTextViewer extends StatefulWidget {
  BookFullTextViewer(
      {Key? key,
      required this.bookCode,
      required this.bookTitle,
      required this.simplified})
      : super(key: key);
  final String bookCode;
  final bool simplified;
  final String bookTitle;
  static const String routeName = '/book/fulltext';

  @override
  State<BookFullTextViewer> createState() => _BookFullTextViewerState();
}

class _BookFullTextViewerState extends State<BookFullTextViewer> {
  final BooksController _booksController = Get.find();

  @override
  void initState() {
    Future.delayed(Duration(milliseconds: 300), (() {
      _booksController.getBookFullText(widget.bookCode, widget.simplified);
    }));
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    Log.debug('build common book full text viewer');

    return Scaffold(
      appBar: AppBar(
        title: Container(
          width: double.infinity,
          height: 40,
          child: Center(child: Text(widget.bookTitle)),
        ),
        leading: BackButton(
          onPressed: () {
            _booksController.bookFullText.value = '';
            Navigator.of(context).pop();
          },
        ),
      ),
      body: SingleChildScrollView(
        scrollDirection: Axis.vertical,
        primary: false,
        child: Padding(
          padding: const EdgeInsets.fromLTRB(50, 16.0, 50, 0),
          child: Column(
            mainAxisAlignment: MainAxisAlignment.start,
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              _booksController.obx(
                (data) => Container(
                  width: Get.width * 0.9,
                  child: HtmlWidget(_booksController.bookFullText.value,
                  textStyle: TextStyle(height: 2, fontSize: 23),
                  ),
                  // Text(_booksController.bookFullText.value,
                  //     style: TextStyle(height: 2, fontSize: 23)),
                ),
                onLoading: Center(
                    child: CircularProgressIndicator(
                  backgroundColor: Get.theme.primaryColorDark,
                  strokeWidth: 9.0,
                )),
              )
            ],
          ),
        ),
      ),
    );
  }
}
